package com.zhangzhan.gulimallsearch.controller;

import com.zhangzhan.gulimallcommcon.tos.es.SkuEsModel;
import com.zhangzhan.gulimallcommcon.util.R;
import com.zhangzhan.gulimallsearch.service.SaveSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.zhangzhan.gulimallcommcon.exception.exception_enum.ExceptionEnum.PRODUCT_SAVE_EXCEPTION;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/6/20 22:58
 */
@RestController
@RequestMapping("/save/es")
@Slf4j
public class SaveSearchController {

    @Autowired
    private SaveSearchService saveSearchService;

    /**
     * 增加商品信息
     */
    @RequestMapping(value = "/product", method = RequestMethod.POST)
    //@RequiresPermissions("gulimallproduct:attrattrgrouprelation:list")
    public R saveProduct(@RequestBody List<SkuEsModel> skuEsModels) {
        Map<String,Object> res = null;
        try {
            res = saveSearchService.saveProduct(skuEsModels);
        } catch (IOException e) {
            return R.error(PRODUCT_SAVE_EXCEPTION.getCode(), PRODUCT_SAVE_EXCEPTION.getMeg());
        }

        List<String>  errIds = (List<String>) res.get("errIds");
        if (!errIds.isEmpty()){
            log.error("商品部分上传失败：{}", Arrays.asList(errIds));
        }

        Boolean failures = (Boolean) res.get("failures");
        return !failures ? R.ok() : R.error();
    }

}
